import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import { jsx as _jsx } from "react/jsx-runtime";
import { InputNumber } from 'antd-v4';
import React from 'react';
/**
 * 数字组件
 *
 * @param FieldDigitProps {
 *     text: number;
 *     moneySymbol?: string; }
 */

var FieldDigit = function FieldDigit(_ref, ref) {
  var text = _ref.text,
      type = _ref.mode,
      render = _ref.render,
      placeholder = _ref.placeholder,
      renderFormItem = _ref.renderFormItem,
      fieldProps = _ref.fieldProps;

  if (type === 'read') {
    var _fieldProps$formatter;

    var fractionDigits = {};

    if (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.precision) {
      fractionDigits = {
        minimumFractionDigits: Number(fieldProps.precision),
        maximumFractionDigits: Number(fieldProps.precision)
      };
    }

    var digit = new Intl.NumberFormat(undefined, _objectSpread(_objectSpread({}, fractionDigits), (fieldProps === null || fieldProps === void 0 ? void 0 : fieldProps.intlProps) || {})).format(Number(text));

    var dom = _jsx("span", {
      ref: ref,
      children: (fieldProps === null || fieldProps === void 0 ? void 0 : (_fieldProps$formatter = fieldProps.formatter) === null || _fieldProps$formatter === void 0 ? void 0 : _fieldProps$formatter.call(fieldProps, digit)) || digit
    });

    if (render) {
      return render(text, _objectSpread({
        mode: type
      }, fieldProps), dom);
    }

    return dom;
  }

  if (type === 'edit' || type === 'update') {
    var _dom = _jsx(InputNumber, _objectSpread({
      ref: ref,
      min: 0,
      placeholder: placeholder
    }, fieldProps));

    if (renderFormItem) {
      return renderFormItem(text, _objectSpread({
        mode: type
      }, fieldProps), _dom);
    }

    return _dom;
  }

  return null;
};

export default /*#__PURE__*/React.forwardRef(FieldDigit);